package com.jcl.recruit.wechat.service;

import com.jcl.core.web.model.ShareUserToken;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.HttpRequest;
import com.jcl.recruit.attach.vo.AttachmentPhotoVo;
import com.jcl.recruit.basic.register.RegisterCallback;
import com.jcl.recruit.basic.register.RegisterUserCallback;
import com.jcl.recruit.basic.register.impl.RecruitRegisterImpl;
import com.jcl.recruit.basic.register.model.RegisterEnum;
import com.jcl.recruit.basic.register.model.RegisterResult;
import com.jcl.recruit.basic.register.vo.RegisterVo;
import com.jcl.recruit.basic.register.vo.RegisterWechatVo;
import com.jcl.recruit.console.logic.ConsoleUserAccessLogic;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.recruit.wechat.logic.WechatCommonLogic;
import com.jcl.recruit.wechat.utils.WechatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WechatRegisterService extends RecruitRegisterImpl {

    @Autowired
    private ConsoleUserAccessLogic consoleUserAccessLogic;

    @Autowired
    private WechatCommonLogic wechatCommonLogic;

    /* *
     * @Author yyliu
     * @Description 微信 企业注册
     * @Date 12:59 2018/9/14
     * @Param [vo]
     * @return com.example.service.register.RegisterResult
     **/
    public RegisterResult corpRegister(RegisterWechatVo wechatVo) {

        wechatVo.setPassword(SystemConfig.getDefaultPassword());
        // 注册企业
        RegisterResult result = super.register(wechatVo, RegisterEnum.WECHAT, new RegisterCallback() {
            @Override
            public void registerCorp(RegisterEnum registerEnum, RegisterVo vo, RegisterResult result) {

                List<ConsoleUserAccess> accessList = consoleUserAccessLogic.findListByExternalKey(wechatVo.getUserKey(),
                        UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT,
                        UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
                ConsoleUserAccess access = null;
                if (CollectionUtils.isNotEmpty(accessList)) {
                    access = accessList.get(0);
                }
                // 清除默认使用企业
                consoleUserAccessLogic.changeUseStatusToInvalid(
                        wechatVo.getUserKey(), UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);

                access.setCorpCode(result.getCorpCode());
                access.setUserId(result.getUserId());
                access.setStatus(UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
                access.setUpdateTime(new Date());
                access.setCreateTime(new Date());
                access.setUsername(access.getExternalKey()
                        + UserExternalLoginConstant.USER_ACCESS_SPLIT + result.getCorpCode());
                consoleUserAccessLogic.updateAccess(access);

                result.setUserAccess(access);
            }
        });
        return result;
    }

    /* *
     * @Author yyliu
     * @Description 微信邀请 用户注册
     * @Date 12:59 2018/9/14
     * @Param [vo]
     * @return com.example.service.register.RegisterResult
     **/
    public RegisterResult userRegister(RegisterWechatVo wechatVo) {
        RegisterResult result = new RegisterResult();
        ShareUserToken tokenInfo = ShareUserToken.decodeInviteToken(wechatVo.getAccessKey());
        wechatVo.setInviteUserId(Integer.parseInt(tokenInfo.getUserId()));
        wechatVo.setPassword(SystemConfig.getDefaultPassword());
        super.addRegisterUser(tokenInfo.getCorpCode(), wechatVo, RegisterEnum.WECHAT, new RegisterUserCallback() {
            @Override
            public void registerUser(RegisterEnum registerEnum, RegisterVo vo, RegisterResult result) {
                ConsoleUserAccess access = consoleUserAccessLogic.findOne(Integer.parseInt(wechatVo.getAccessId()));
                if (null != access) {
                    // 清除默认使用企业
                    consoleUserAccessLogic.changeUseStatusToUnused(
                            access.getExternalKey(), UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);

                    access.setCorpCode(result.getCorpCode());
                    access.setUserId(result.getUserId());
                    access.setUsername(access.getExternalKey()
                            + UserExternalLoginConstant.USER_ACCESS_SPLIT + result.getCorpCode());
                    // 默认使用注册企业
                    access.setStatus(UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
                    access.setUpdateTime(new Date());
                    consoleUserAccessLogic.updateAccess(access);
                    // 获取登录的微信用户信息
                    WechatUtils.WechatUserInfo userInfo = WechatUtils.getUserBaseInfoObject(WechatUtils.getAccessToken(),
                            access.getExternalKey());
                    if (null != userInfo && null != userInfo.getHeadimgurl()) {
                        byte[] b = HttpRequest.getByte(userInfo.getHeadimgurl());
                        AttachmentPhotoVo photoVo = new AttachmentPhotoVo();
                        photoVo.setBytes(b);
                        photoVo.setFileName(userInfo.getOpenid());
                        // 更换头像
                        wechatCommonLogic.updateUserHead(photoVo, result.getUserId(), result.getCorpCode());
                    }
                }
                result.setUserAccess(access);
            }
        });
        return result;
    }
}
